package com.sj.mapper;

import com.sj.entity.Menu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author 小七
 * @since 2021-07-30
 */
@Repository
public interface MenuMapper extends BaseMapper<Menu> {

    /**
     * 获取用户的菜单列表
     *
     * @param userId 用户id
     * @return 菜单列表
     */
    List<Menu> getMenuListByUserId(Integer userId);

    /**
     * 获取菜单列表
     *
     * @param parentId 父菜单id 当为0时 为最高一级菜单
     * @return 菜单列表
     */
    @Select("select * from menu where parent_id = #{parentId} and status = 1 order by order_num asc")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "id", property = "childMenuList", many = @Many(select = "com.sj.mapper.MenuMapper.getMenuListByParentId", fetchType = FetchType.EAGER))
    })
    List<Menu> getAllMenuList(Integer parentId);

    /**
     * 根据 父菜单id 查询菜单列表
     *
     * @param parentId 父菜单id
     * @return 菜单列表
     */
    @Select("<script>" +
            "select * from menu " +
            "<where>" +
            "<if test='parentId != null'>" +
            " and parent_id = #{parentId}" +
            "</if>" +
            " and status = 1 " +
            "</where>" +
            "order by order_num asc" +
            "</script>")
    List<Menu> getMenuListByParentId(Integer parentId);

}
